import { createI18n } from "vue-i18n";
import loadLocaleMessages from "./loader";
import { storage } from "@/utils/storage";
import type { App } from "vue";

// 创建 i18n 实例
const i18n = createI18n({
  legacy: false, // 使用 Composition API
  locale: storage.get("language") || "zhCN", // 默认语言
  fallbackLocale: "zhCN", // 回退语言
  messages: {}, // 初始为空，动态加载
});

// 动态加载翻译资源
let messages = await loadLocaleMessages();
Object.keys(messages).forEach((locale) => {
  i18n.global.setLocaleMessage(locale, messages[locale]);
});
const setupI18n = (app: App<Element>) => {
  app.use(i18n);
};
export { i18n, setupI18n };
